package project.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import project.pojo.Course;
import project.pojo.Teacher;
import project.service.TeacherService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addCourseServlet")
public class AddCourseServlet extends HttpServlet {
    private TeacherService service = new TeacherService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String teacherId = request.getParameter("teacherId");
        Teacher teacher = service.selectTeacherByTeacherId(Long.parseLong(teacherId));

        BufferedReader reader = request.getReader();
        String s = reader.readLine();

        Course course = JSON.parseObject(s, Course.class);
        course.setDepartmentName(teacher.getDepartmentName());
        course.setTeacherId(Long.parseLong(teacherId));

        System.out.println(course);

        boolean flag = service.addCourse(course);

        if(flag){
            response.getWriter().write("success");
        }else{
            response.getWriter().write("unsuccess");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
